/**
 * 
 */
package com.mnurre.shopping.assistant;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

/**
 * @author michael
 *
 */
public class EditCategoryActivity extends Activity implements OnClickListener {
	
	private static final String TAG = "EditCategoryActivity";
	private EditText categoryEditText;
	private CheckBox isTaxableCheckBox;
	private Button saveCategoryButton;
	private CategoriesData mCategoriesData;
	private Long mCategoryId;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Creating activity.");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_category);
		setTitle(R.string.category_add_activity_label);
		
		Log.d(TAG, "Getting references to UI components.");
		categoryEditText = (EditText)findViewById(R.id.categoryEditText);
		isTaxableCheckBox = (CheckBox)findViewById(R.id.isTaxableCheckBox);
		saveCategoryButton = (Button)findViewById(R.id.saveCategoryButton);
		saveCategoryButton.setOnClickListener(this);
		
		Log.d(TAG, "Creating a new instance of the CategoriesData.");
		mCategoriesData = new CategoriesData(this);
		
		Log.d(TAG, "Checking extras to see if we are editing.");
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			Log.d(TAG, "We have extras, so we are editing.");
			mCategoryId = extras.getLong(DataManager.Tables.Categories.COLUMN_ID);
			Log.d(TAG, "Category Id is " + mCategoryId);
			Log.d(TAG, "Calling populate fields.");
			populateFields();
		} else {
			Log.d(TAG, "We do not have extras, so setting mCategoryId to null.");
			mCategoryId = null;
		}
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Log.d(TAG, "Handling the save button's click event.");
		String name = categoryEditText.getText().toString();
		int taxable = isTaxableCheckBox.isChecked() ? 1 : 0;
		Log.d(TAG, "Opening data and adding the category.");
		mCategoriesData.open();
		Log.d(TAG, "Checking to see if mCategoryId is null.");
		if(mCategoryId == null){
			Log.d(TAG, "mCategoryId is null so saving a new category.");
			mCategoriesData.insertCategory(name, taxable);
		} else {
			Log.d(TAG, "mCategoryId is not null, so saving an update.");
			mCategoriesData.updateCategory(mCategoryId, name, taxable);
		}
		Log.d(TAG, "Closing the data source.");
		mCategoriesData.close();
		Log.d(TAG, "Setting Result to RESULT_OK");
		setResult(RESULT_OK);
		Log.d(TAG, "Calling finish on current activity.");
		finish();
	}

	private void populateFields(){
		Log.d(TAG, "populateFields called. Opening data.");
		mCategoriesData.open();
		Log.d(TAG, "Getting a cursor for category.");
		Cursor cursor = mCategoriesData.getCategoryById(mCategoryId);
		Log.d(TAG, "Calling startManagingCursor()");
		startManagingCursor(cursor);
		Log.d(TAG, "Setting the edit text field.");
		categoryEditText.setText(cursor.getString(cursor.getColumnIndex(DataManager.Tables.Categories.COLUMN_NAME)));
		Log.d(TAG, "Getting the taxable value.");
		int taxable = cursor.getInt(cursor.getColumnIndex(DataManager.Tables.Categories.COLUMN_IS_TAXABLE));
//		Log.d(TAG, "Closing the data source.");
//		mCategoriesData.close();
		Log.d(TAG, "Setting the isTaxable field.");
		isTaxableCheckBox.setChecked(taxable == 0 ? false : true);
	}
}
